﻿//-----------------------------------------------------------------------
// <copyright file="DataBagValueTypeUtility.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.DataBag
{
    using System;
    using System.Collections.Generic;
    using TileTool.Common.Math;

    /// <summary>
    /// Utility class which manages common data bag type functionality.
    /// </summary>
    public static class DataBagValueTypeUtility
    {
        #region Fields - Private - Static

        /// <summary>
        /// Type mapping dictionary.
        /// </summary>
        private static IDictionary<Type, DataBagValueType> typeDict = new Dictionary<Type, DataBagValueType>();

        #endregion

         #region Constructor

        /// <summary>
        /// Class static constructor.
        /// </summary>
        static DataBagValueTypeUtility()
        {
            BuildTypeDictionary();
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Tests to see if the passed type is a valid data bag value type.
        /// </summary>
        /// <param name="type">Type to test.</param>
        /// <returns>Returns true if the passed type is a valid data bag value type.</returns>
        public static bool IsValidType(Type type)
        {
            if (type == null)
            {
                return false;
            }

            if (!typeDict.ContainsKey(type))
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// Given a C# type, get a data bag value type from it.
        /// </summary>
        /// <param name="type">Type to convert to a bag value type.</param>
        /// <returns>Returns the bag value type associated with the passed type.</returns>
        public static DataBagValueType GetDataBagValueTypeFromType(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type", "Type must be non-null.");
            }

            if (!IsValidType(type))
            {
                throw new InvalidOperationException("Can only get bag type if input type is valid.");
            }

            return typeDict[type];
        }

        #endregion

        #region Methods - Private

        /// <summary>
        /// Build up our supported type mapping dictionary.
        /// </summary>
        private static void BuildTypeDictionary()
        {
            AddToTypeDictionary<int>(DataBagValueType.Int);
            AddToTypeDictionary<uint>(DataBagValueType.UInt);
            AddToTypeDictionary<long>(DataBagValueType.Long);
            AddToTypeDictionary<ulong>(DataBagValueType.ULong);
            AddToTypeDictionary<float>(DataBagValueType.Float);
            AddToTypeDictionary<string>(DataBagValueType.String);
            AddToTypeDictionary<ColorRGBA>(DataBagValueType.ColorRGBA);
            AddToTypeDictionary<Vector2D>(DataBagValueType.Vector2D);
            AddToTypeDictionary<Matrix2D>(DataBagValueType.Matrix2D);
            AddToTypeDictionary<Size2D>(DataBagValueType.Size2D);
        }

        /// <summary>
        /// Add the passed type to the dictionary.
        /// </summary>
        /// <typeparam name="AddType">C# type to add.</typeparam>
        /// <param name="type">Data bag value type to associate with the C# Type.</param>
        private static void AddToTypeDictionary<AddType>(DataBagValueType type)
        {
            typeDict.Add(typeof(AddType), type);
        }

        #endregion
    }
}
